package cloud.shyidong.admin.tenant.service;

import cloud.shyidong.core.exception.BusinessException;
import cloud.shyidong.core.request.IdsRequest;
import cloud.shyidong.core.request.LongIdRequest;
import cloud.shyidong.core.service.AbstractService;
import cloud.shyidong.core.util.CollectionUtils;
import cloud.shyidong.core.util.ObjectUtils;
import cloud.shyidong.tenant.constant.TenantStatusConstant;
import cloud.shyidong.tenant.dao.*;
import cloud.shyidong.tenant.entity.*;
import cloud.shyidong.tenant.request.TenantTenantInsertRequest;
import cloud.shyidong.tenant.request.TenantTenantPageRequest;
import cloud.shyidong.tenant.request.TenantTenantUpdateRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租户业务处理
 * </p>
 *
 * @author syd
 * @since 2023-09-18 15:34:26
 */
@Service
public class TenantTenantService extends AbstractService<TenantTenantDao, TenantTenantEntity, LongIdRequest, TenantTenantInsertRequest, TenantTenantUpdateRequest, TenantTenantPageRequest> {
    
    private TenantTenantMenuDao tenantTenantMenuDao;
    
    private TenantMenuDao tenantMenuDao;
    
    private TenantTenantFunctionDao tenantTenantFunctionDao;
    
    private TenantFunctionDao tenantFunctionDao;
    
    @Autowired
    public void setTenantTenantMenuDao(TenantTenantMenuDao tenantTenantMenuDao) {
        this.tenantTenantMenuDao = tenantTenantMenuDao;
    }
    
    @Autowired
    public void setTenantMenuDao(TenantMenuDao tenantMenuDao) {
        this.tenantMenuDao = tenantMenuDao;
    }
    
    @Autowired
    public void setTenantTenantFunctionDao(TenantTenantFunctionDao tenantTenantFunctionDao) {
        this.tenantTenantFunctionDao = tenantTenantFunctionDao;
    }
    
    @Autowired
    public void setTenantFunctionDao(TenantFunctionDao tenantFunctionDao) {
        this.tenantFunctionDao = tenantFunctionDao;
    }
    
    @Override
    public void afterInsert(TenantTenantInsertRequest insertRequest, TenantTenantEntity tenantTenantEntity) {
        
        insertMenu(tenantTenantEntity.getId(), insertRequest.getMenuIds());
        
        insertFunction(tenantTenantEntity.getId(), insertRequest.getFunctionIds());
        
    }
    
    @Override
    public void afterUpdate(TenantTenantUpdateRequest updateRequest, TenantTenantEntity tenantTenantEntity) {
        
        tenantTenantMenuDao.delete(new UpdateWrapper<TenantTenantMenuEntity>().eq("tenant_id", tenantTenantEntity.getId()));
        
        insertMenu(tenantTenantEntity.getId(), updateRequest.getMenuIds());
        
        tenantTenantFunctionDao.delete(new UpdateWrapper<TenantTenantFunctionEntity>().eq("tenant_id", tenantTenantEntity.getId()));
        
        insertFunction(tenantTenantEntity.getId(), updateRequest.getFunctionIds());
        
    }
    
    private void insertMenu(Long id, List<Long> menuIds) {
        
        if (CollectionUtils.isEmpty(menuIds)) {
            return;
        }
        
        for (Long menuId : menuIds) {
            TenantTenantMenuEntity tenantTenantMenuEntity = new TenantTenantMenuEntity();
            tenantTenantMenuEntity.setTenantId(id);
            tenantTenantMenuEntity.setMenuId(menuId);
            tenantTenantMenuDao.insert(tenantTenantMenuEntity);
        }
        
    }
    
    private void insertFunction(Long id, List<Long> functionIds) {
        
        if (CollectionUtils.isEmpty(functionIds)) {
            return;
        }
        
        for (Long functionId : functionIds) {
            TenantTenantFunctionEntity tenantTenantFunctionEntity = new TenantTenantFunctionEntity();
            tenantTenantFunctionEntity.setTenantId(id);
            tenantTenantFunctionEntity.setFunctionId(functionId);
            tenantTenantFunctionDao.insert(tenantTenantFunctionEntity);
        }
        
    }
    
    public Map<String, Object> approvals(IdsRequest<LongIdRequest> idsRequest) {
        
        Integer count = 0;
        for (LongIdRequest longIdRequest : idsRequest.getIds()) {
            
            TenantTenantEntity tenantTenantEntity = getDao().selectById(longIdRequest.getId());
            if (ObjectUtils.isNull(tenantTenantEntity)) {
                throw BusinessException.from("未找到要试用的记录");
            }
            
            if (tenantTenantEntity.getStatus().intValue() != TenantStatusConstant.EXPIRE) {
                continue;
            }
            
            tenantTenantEntity.setStatus(TenantStatusConstant.NOT_AUTH);
            if (getDao().updateById(tenantTenantEntity) > 0) {
                count++;
            }
            
        }
        
        if (count.intValue() == 0) {
            throw BusinessException.from("批量试用失败");
        }
        
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("count", count);
        return result;
    }
    
    public Map<String, Object> auths(IdsRequest<LongIdRequest> idsRequest) {
        
        Integer count = 0;
        for (LongIdRequest longIdRequest : idsRequest.getIds()) {
            
            TenantTenantEntity tenantTenantEntity = getDao().selectById(longIdRequest.getId());
            if (ObjectUtils.isNull(tenantTenantEntity)) {
                throw BusinessException.from("未找到要试用的记录");
            }
            
            if (tenantTenantEntity.getStatus().intValue() == TenantStatusConstant.AUTH || tenantTenantEntity.getStatus().intValue() == TenantStatusConstant.DISABLE) {
                continue;
            }
            
            tenantTenantEntity.setStatus(TenantStatusConstant.AUTH);
            if (getDao().updateById(tenantTenantEntity) > 0) {
                count++;
            }
            
        }
        
        if (count.intValue() == 0) {
            throw BusinessException.from("批量试用失败");
        }
        
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("count", count);
        return result;
    }
    
    @Override
    public Map<String, Object> view(LongIdRequest idRequest) {
        
        Map<String, Object> result = super.view(idRequest);
        List<TenantTenantMenuEntity> tenantTenantMenuEntities = tenantTenantMenuDao.selectList(new QueryWrapper<TenantTenantMenuEntity>().eq("tenant_id", idRequest.getId()));
        if (CollectionUtils.isNotEmpty(tenantTenantMenuEntities)) {
            List<TenantMenuEntity> tenantMenuEntities = new ArrayList<>();
            for (TenantTenantMenuEntity tenantTenantMenuEntity : tenantTenantMenuEntities) {
                TenantMenuEntity tenantMenuEntity = tenantMenuDao.selectById(tenantTenantMenuEntity.getMenuId());
                tenantMenuEntities.add(tenantMenuEntity);
            }
            result.put("menus", tenantMenuEntities);
            
        }
        List<TenantTenantFunctionEntity> tenantTenantFunctionEntities = tenantTenantFunctionDao.selectList(new QueryWrapper<TenantTenantFunctionEntity>().eq("tenant_id", idRequest.getId()));
        if (CollectionUtils.isNotEmpty(tenantTenantFunctionEntities)) {
            List<TenantFunctionEntity> systemFunctionEntities = new ArrayList<>();
            for (TenantTenantFunctionEntity tenantTenantFunctionEntity : tenantTenantFunctionEntities) {
                TenantFunctionEntity tenantFunctionEntity = tenantFunctionDao.selectById(tenantTenantFunctionEntity.getFunctionId());
                systemFunctionEntities.add(tenantFunctionEntity);
            }
            result.put("functions", systemFunctionEntities);
        }
        return result;
        
    }
    
}
